package com.rika.controller;

import com.rika.pojo.*;
import com.rika.response.QueryResult;
import com.rika.response.Result;
import com.rika.service.*;
import com.rika.util.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PomainController {
    @Resource
    private PomainService pomainService;
    @Resource
    private PoItemService poItemService;
    @Resource
    private VenderService venderService;
    @Resource
    private ProductService productService;
    @Resource
    private StockRecordService stockRecordService;
    @Resource
    private PayRecordService payRecordService;

    @RequestMapping("getAllPomain")
    public QueryResult selectAllPomain(Integer currentPage,Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<PoMain> list=pomainService.selectAllPomain(params);
        int total=pomainService.countAll1();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("getAllPomainForSettle")
    public QueryResult selectAllPomainForSettle(Integer currentPage,Integer pageSize,String payType){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("payType",payType);
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<PoMain> list=pomainService.selectAllPomainForSettle(params);
        int total=pomainService.selectAllPomainForSettleCountAll(payType);
        System.out.println(list);
        System.out.println(total);
        return QueryResult.ok(total,list);
    }
    @RequestMapping("addPomain")
    public Result addPomain(PoMain pomain,String poItemLists,String venderName){
        Result result=new Result();
        List<Vender> venderCodelist=venderService.selectVenderByName(venderName);
        String venderCode=venderCodelist.get(0).getVenderCode();
        pomain.setVenderCode(venderCode);
        pomainService.addPomain(pomain);
        String[] list=poItemLists.split(":");
        String[] list3= new String[2*list.length];
        for(int i=0;i<list.length;i++){
            String[] list2=list[i].split(",");
            for(int j=0;j<list2.length;j++){
                list3[i*2+j]=list2[j];
            }
        }
        String[] list4=new String[list3.length/2];
        for(int i=0;i<list3.length;i+=2){
            list3[i]=list3[i].replace("\"","");
            list3[i]=list3[i].replace("{","");
            list3[i]=list3[i].replace("[","");
            list3[i]=list3[i].replace("}","");
            list3[i]=list3[i].replace("]","");
            list4[i/2]=list3[i];
        }
        String[] list5=new String[list4.length-1];
        for(int i=1;i<list4.length;i++){
            list5[i-1]=list4[i];
        }
        int t=list5.length;
        int s=t/6;
        while (t!=0){
            PoItem poItem=new PoItem();
            for(int n=0;n<s;n++){
                System.out.println(list5[0+n*6]+" "+list5[1+n*6]+" "+list5[2+n*6]+" "+list5[3+n*6]+" "+list5[4+n*6]+" "+list5[5+n*6]);
                poItem.setPoId(Long.parseLong(list5[0+n*6]));
                poItem.setProductCode(list5[1+n*6]);
                poItem.setItemPrice(Float.parseFloat(list5[2+n*6]));
                poItem.setUnitName(list5[3+n*6]);
                poItem.setNum(Integer.parseInt(list5[4+n*6]));
                poItem.setUnitPrice(Float.parseFloat(list5[5+n*6]));
                t-=6;
                poItemService.addPoItem(poItem);
            }
        }
        List<PoItem> poItemList=poItemService.selectAllPoItemByPoId(pomain.getPoId());
        for(int i=0;i<poItemList.size();i++){
            Map<String,Object> params=new HashMap<>();
            List<Product> list2=productService.selectByProductCode(poItemList.get(i).getProductCode());
            params.put("poNum",list2.get(0).getPoNum()+poItemList.get(i).getNum());
            params.put("productCode",poItemList.get(i).getProductCode());
            productService.updateProductWhenAddPoMain(params);
        }
        result.setCode(200);
        result.setMsg("新增成功");
        return result;
    }
    @RequestMapping("deletePomain")
    public Result deletePoId(Long poId){
        Result result=new Result();
        poItemService.deletePoItem(poId);
        pomainService.deletePoMain(poId);
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }
    @RequestMapping("selectByPoId")
    public Result selectByPoId(Long poId){
        Result result=new Result();
        List<PoMain> poMainList=pomainService.selectByPoId(poId);
        List<PoItem> poItemList=poItemService.selectAllPoItemList(poId);
        poMainList.get(0).setPoItemList(poItemList);
        result.setCode(200);
        result.setData(poMainList);
        return result;
    }
    @RequestMapping("updatePomain")
    public Result updatePomain(PoMain pomain,String poItemLists){
        Result result=new Result();
        pomainService.updatePoMain(pomain);
        poItemService.deletePoItem(pomain.getPoId());
        String[] list=poItemLists.split(":");
        String[] list3= new String[2*list.length];
        for(int i=0;i<list.length;i++){
            String[] list2=list[i].split(",");
            for(int j=0;j<list2.length;j++){
                list3[i*2+j]=list2[j];
            }
        }
        String[] list4=new String[list3.length/2];
        for(int i=0;i<list3.length;i+=2){
            list3[i]=list3[i].replace("\"","");
            list3[i]=list3[i].replace("{","");
            list3[i]=list3[i].replace("[","");
            list3[i]=list3[i].replace("}","");
            list3[i]=list3[i].replace("]","");
            list4[i/2]=list3[i];
        }
        String[] list5=new String[list4.length-1];
        for(int i=1;i<list4.length;i++){
            list5[i-1]=list4[i];
        }
        int t=list5.length;
        int s=t/6;
        while (t!=0){
            PoItem poItem=new PoItem();
            for(int n=0;n<s;n++){
                //System.out.println(list5[0+n*6]+" "+list5[1+n*6]+" "+list5[2+n*6]+" "+list5[3+n*6]+" "+list5[4+n*6]+" "+list5[5+n*6]);
                poItem.setPoId(Long.parseLong(list5[0+n*6]));
                poItem.setProductCode(list5[1+n*6]);
                poItem.setItemPrice(Float.parseFloat(list5[2+n*6]));
                poItem.setUnitName(list5[3+n*6]);
                poItem.setNum(Integer.parseInt(list5[4+n*6]));
                poItem.setUnitPrice(Float.parseFloat(list5[5+n*6]));
                t-=6;
                poItemService.addPoItem(poItem);
            }
        }
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }

    @RequestMapping("settlePomain")
    public Result settlePomain(Long poId,String endUser){
        Result result=new Result();
        Map<String,Object> params=new HashMap<>();
        params.put("poId",poId);
        params.put("endUser",endUser);
        pomainService.settlePoMain(params);
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }
    @RequestMapping("pomainQuery")
    public Result pomainQuery(String poId,String date,Integer status){
        Result result=new Result();
        Map<String,Object> params=new HashMap<>();
        String startTime="";
        String endTime="";
        if(date.length()>0){
            String[] Date=date.split("~");
            startTime=Date[0].trim();
            endTime=Date[1].trim();
        }
        params.put("poId",poId);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("status",status);
        List<PoMain> list=pomainService.selectPoMainQuery(params);
        result.setCode(200);
        result.setData(list);
        return result;
    }
    @RequestMapping("selectAllPomainNotInStore")
    public QueryResult selectAllPomainNotInStore(Integer currentPage,Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<PoMain> list=pomainService.selectPoMainNotInStore(params);
        int total=pomainService.selectPoMainNotInStoreCount();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("updatePoMainInStore")
    public Result updatePoMainInStore(Long poId,StockRecord stockRecord){
        Result result=new Result();
        Map<String,Object> params2=new HashMap<>();
        params2.put("poId",poId);
        params2.put("stockTime",stockRecord.getStockTime());
        params2.put("stockUser",stockRecord.getCreateUser());
        pomainService.updatePoMainToInStore(params2);
        List<PoItem> list=poItemService.selectAllPoItemByPoId(poId);
        for(int i=0;i<list.size();i++){
            Map<String,Object> params=new HashMap<>();
            List<Product> list2=productService.selectByProductCode(list.get(i).getProductCode());
            params.put("num",list.get(i).getNum()+list2.get(0).getNum());
            params.put("poNum",list2.get(0).getPoNum()-list.get(i).getNum());
            params.put("productCode",list.get(i).getProductCode());
            productService.updateproductInStore(params);
            stockRecord.setProductCode(list.get(i).getProductCode());
            stockRecord.setStockNum(list.get(i).getNum());
            stockRecord.setStockType(2);
            stockRecordService.addStockRecord(stockRecord);
        }
        result.setCode(200);
        result.setMsg("入库成功");
        return result;
    }
    @RequestMapping("selectPoMainExpend1")
    public QueryResult selectPoMainExpend1(Integer currentPage, Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<PoMain> list=pomainService.selectPoMainExpend1(params);
        int total=pomainService.selectPoMainExpend1CountAll();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("updateexpend1")
    public Result updateexpend1(PoMain poMain,String pay_Time,String account,String orderCode,String pay_Type){
        PayRecord payRecord=new PayRecord();
        payRecord.setAccount(account);
        payRecord.setOrderCode(orderCode);
        payRecord.setPay_Time(pay_Time);
        payRecord.setPay_Type(1);
        Result result=new Result();
        pomainService.updateexpend1(poMain);
        List<PoMain> list=pomainService.selectByPoId(poMain.getPoId());
        payRecord.setPay_price(list.get(0).getPoTotal());
        payRecordService.addPayRecord(payRecord);
        result.setCode(200);
        result.setMsg("付款成功");
        return result;
    }
    @RequestMapping("selectPoMainExpend2")
    public QueryResult selectPoMainExpend2(Integer currentPage, Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<PoMain> list=pomainService.selectPoMainExpend2(params);
        int total=pomainService.selectPoMainExpend2CountAll();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("updateexpend2")
    public Result updateexpend2(PoMain poMain,String pay_Time,String account,String orderCode,String pay_Type){
        PayRecord payRecord=new PayRecord();
        payRecord.setAccount(account);
        payRecord.setOrderCode(orderCode);
        payRecord.setPay_Time(pay_Time);
        payRecord.setPay_Type(2);
        Result result=new Result();
        pomainService.updateexpend1(poMain);
        List<PoMain> list=pomainService.selectByPoId(poMain.getPoId());
        payRecord.setPay_price(list.get(0).getPoTotal());
        payRecordService.addPayRecord(payRecord);
        result.setCode(200);
        result.setMsg("付款成功");
        return result;
    }
    @RequestMapping("selectPoMainExpend3")
    public QueryResult selectPoMainExpend3(Integer currentPage, Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<PoMain> list=pomainService.selectPoMainExpend3(params);
        int total=pomainService.selectPoMainExpend3CountAll();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("updateexpend3")
    public Result updateexpend3(PoMain poMain,String pay_Time,String account,String orderCode,String pay_Type){
        PayRecord payRecord=new PayRecord();
        payRecord.setAccount(account);
        payRecord.setOrderCode(orderCode);
        payRecord.setPay_Time(pay_Time);
        Result result=new Result();
        List<PoMain> list=pomainService.selectByPoId(poMain.getPoId());
        if(list.get(0).getStatus()==1){
            payRecord.setPay_price(list.get(0).getPrePayFee());
            pomainService.updateexpend2(poMain);
            payRecord.setPay_Type(3);
        }
        //尾款
        if(list.get(0).getStatus()==2){
            payRecord.setPay_price(list.get(0).getPoTotal()-list.get(0).getPrePayFee());
            pomainService.updateexpend1(poMain);
            payRecord.setPay_Type(4);
        }
        payRecordService.addPayRecord(payRecord);
        result.setCode(200);
        result.setMsg("付款成功");
        return result;
    }
    @RequestMapping("selectPoMainNum")
    public Result selectPoMainNum(String date){
        Result result=new Result();
        String[] Date=date.split("~");
        Map<String,Object> params=new HashMap<>();
        params.put("startTime",Date[0].trim());
        params.put("endTime",Date[1].trim());
        List<PoMain> pomainNum=pomainService.selectPoMainNum(params);
        result.setCode(200);
        result.setData(pomainNum);
        return result;
    }
    @RequestMapping("selectEndNum")
    public Result selectEndNum(String date){
        Result result=new Result();
        String[] Date=date.split("~");
        Map<String,Object> params=new HashMap<>();
        params.put("startTime",Date[0].trim());
        params.put("endTime",Date[1].trim());
        Integer endNum=pomainService.selectEndNum(params);
        result.setCode(200);
        result.setData(endNum);
        return result;
    }
    @RequestMapping("selectEndPomainPrice")
    public Result selectEndPomainPrice(String date){
        Result result=new Result();
        String[] Date=date.split("~");
        Map<String,Object> params=new HashMap<>();
        params.put("startTime",Date[0].trim());
        params.put("endTime",Date[1].trim());
        List<PoMain> endPomainPrice=pomainService.selectEndPomainPrice(params);
        result.setCode(200);
        result.setData(endPomainPrice);
        return result;
    }
    @RequestMapping("selectPoMainReportList")
    public Result selectPoMainReportList(String date){
        Result result=new Result();
        String[] Date=date.split("~");
        Map<String,Object> params=new HashMap<>();
        params.put("startTime",Date[0].trim());
        params.put("endTime",Date[1].trim());
        List<PoMain> poMainReportList=pomainService.selectPoMainReportList(params);
        result.setCode(200);
        result.setData(poMainReportList);
        return result;
    }
}
